package controllers.dbWrapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import controllers.Checks;

public class InstallationWrapper {
	
	public static void insertSystemEmployee() throws SQLException, Exception{
	    int generatedID=0;
	    ResultSet rs;
		String personQuery = "INSERT INTO `"+DBWrapper.getDbname()+"`.`person` (`tax_code`, `name`, `surname`, `address`, `email`, `fax`, `phone_number`,`birth_date`) VALUES ('ADM0', 'System', 'Admin', '-', 'admin@caternina.net', '0', '0',0);";
	    String employeeQuery="INSERT INTO `"+DBWrapper.getDbname()+"`.`employee` (`tax_code`, `company_referent`, `role`, `salary`, `birth_date`) VALUES ('ADM0', 'ADM0', 'HUMAN_RESOURCES_RESPONSABLE', '0', '0');";
	    Statement stmt = DBWrapper.getConnection().createStatement();
	    stmt.executeUpdate(personQuery);
	    stmt.executeUpdate(employeeQuery, Statement.RETURN_GENERATED_KEYS);
	    rs= stmt.getGeneratedKeys();
	    rs.next();
	    generatedID = rs.getInt(1);
	    rs.close();
	    String contractQuery="INSERT INTO `"+DBWrapper.getDbname()+"`.`employment_contract`(`id_employment_contract`,`employee_tax_code`,`role`,`agency_responsable`,`salary`,`starting_date`,`ending_date`,`terms`,`is_temporary`) VALUES("+generatedID+",'ADM0','HUMAN_RESOURCES_RESPONSABLE','ADM0',0,0,0,'none',0);";
	    stmt.executeUpdate(contractQuery);
	    String userQuery="INSERT INTO `"+DBWrapper.getDbname()+"`.`user` (`user_name`, `password`, `id_user`, `user_type`) VALUES ('SYS-ADMIN', 'SYSADM', "+generatedID+", 'employee');";
	    stmt.executeUpdate(userQuery);
	}
	
	public static void setSystemLogin(String userName,String password) throws Exception{
		Checks.checkUser(userName,password,true);
		String settingQuery="UPDATE `"+DBWrapper.getDbname()+"`.`user` SET `user_name`='"+userName+"', `password`='"+password+"' WHERE `user_name`='SYS-ADMIN';";
	    Statement stmt = DBWrapper.getConnection().createStatement();
	    stmt.executeUpdate(settingQuery);
	}
}
